package com.doit.day01.op

//fold  都是用来做聚合得 并且他也分 foldLeft   foldRight
object FoldDemo {
  def main(args: Array[String]): Unit = {

    val list: List[Int] = List(1, 2, 3, 4, 5, 6, 7)

    //def fold[A1 >: A](z: A1)(op: (A1, A1) => A1): A1 = foldLeft(z)(op)
    //用fold和reduce唯一得区别就是，我的fold可以加一个初始值
    val i: Int = list.fold(10)((a: Int, b: Int) => a + b)
    println(i)//38


    val i1: Int = list.foldRight(10)((a: Int, b: Int) => a + b)
    println(i1)//38


    val i3: Int = list.fold(10)((a: Int, b: Int) => a - b)
    println(i3) //-18


    // 7-10 = -3  6 - -3  = 9   -4   8 -5  7   1- 7 = -6
    val i4: Int = list.foldRight(10)((a: Int, b: Int) => a - b)
    println(i4)//-6








  }

}
